package com.dms.web.controller.system;

import java.util.List;

import com.dms.common.core.domain.Ztree;
import com.dms.system.domain.dto.SysAreaDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysArea;
import com.dms.system.service.ISysAreaService;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;

/**
 * 设备所属区域Controller
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Controller
@RequestMapping("/system/area")
public class SysAreaController extends BaseController
{
    private String prefix = "system/area";

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询设备所属区域列表
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysArea sysArea)
    {
        // page helper 开启分页
        startPage();
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return getDataTable(list);
    }

    /**
     * 导出设备所属区域列表
     */
    @RequiresPermissions("system:area:export")
    @Log(title = "设备所属区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        return util.exportExcel(list, "area");
    }

    /**
     * 新增设备所属区域
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备所属区域
     */
    @RequiresPermissions("system:area:add")
    @Log(title = "设备所属区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysArea sysArea)
    {
        return toAjax(sysAreaService.insertSysArea(sysArea));
    }

    /**
     * 修改设备所属区域
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysArea sysArea = sysAreaService.selectSysAreaById(id);
        mmap.put("sysArea", sysArea);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备所属区域
     */
    @RequiresPermissions("system:area:edit")
    @Log(title = "设备所属区域", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysArea sysArea)
    {
        return toAjax(sysAreaService.updateSysArea(sysArea));
    }

    /**
     * 删除设备所属区域
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "设备所属区域", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAreaService.deleteSysAreaByIds(ids));
    }


    /**
     * 加载区域设备列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(){
        return sysAreaService.selectAreaTree(null);
    }

    @GetMapping("/getAreaPlants")
    @ResponseBody
    public List<SysAreaDTO> getAreaPlantsByMissionId(Long missionId){
        return sysAreaService.selectAreaPlantsByMissionId(missionId);
    }
}
